package gov.pbc.nn.kjgl.software.dao;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import gov.pbc.nn.core.dao.AbstractHibernateDao;
import gov.pbc.nn.kjgl.archive.dao.ArchivesInputDao;
import gov.pbc.nn.kjgl.entity.Project;
import gov.pbc.nn.kjgl.entity.ProjectFile;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Criterion;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.data.provider.filter.FilterCriterion;

/**
 * 操作文件宝贝的dao文件
 * 
 * @author jack.zhao
 * 
 */
@Repository
public class ProjectBackupDao extends AbstractHibernateDao {

	@Autowired
	private ArchivesInputDao archivesInputDao;

	/**
	 * 分页得到所有的Porjcet，已经可以立项的porject
	 * 
	 * @auther jack.zhao
	 * @param page
	 * 
	 */
	public void getPageAll(Page<Project> page, Criteria criteria) {

		String hql = "select distinct projectId from ProjectMember where  invalid=false and username=:username";
		String username = ContextHolder.getLoginUser().getUsername();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("username", username);
		List<String> ls = this.query(hql, map);
		if (ls.size() > 0) {
			DetachedCriteria query = this.buildDetachedCriteria(criteria, Project.class);
			query.add(Restrictions.or(Restrictions.eq("invalid", false), Restrictions.isNull("invalid")));
			query.add(Restrictions.eq("state", 10));
			query.add(Restrictions.in("id", ls));
			this.pagingQuery(page, query);
		}
		
	}

	/**
	 * 根据id得到ProjectFile
	 * 
	 * @auther jack.zhao
	 * @param pid
	 * @return
	 * 
	 */
	public ProjectFile getByProjectId(String pid) {
		String hql = "from ProjectFile where invalid=false and id=:id";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", pid);
		List<ProjectFile> pf = this.query(hql, map);
		if (pf.isEmpty()) {
			return new ProjectFile();
		} else {
			return (ProjectFile) pf.get(0);
		}
	}

	public synchronized boolean check(String filename, String path) {
		String hql = "select COUNT(*) from ProjectFile where  invalid=false and fileName=:fileName and path=:path ";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("fileName", filename);
		map.put("path", path);
		int count = this.queryForInt(hql, map);
		if (count == 0) {
			return true;
		} else {
			return false;
		}
	}

	public void saveProjectFile(String pid, String filename, String pathid) {
		ProjectFile entity = new ProjectFile();
		entity.setPath(pathid);
		entity.setFileName(filename);
		entity.setOwnerId(pid);
		this.save(entity);
	}

	public void deleteProjectFile(ProjectFile pf) throws IOException {
		Path path = Paths.get(pf.getPath());
		this.delete(pf);
		Files.deleteIfExists(path);
	}

	public void delProjectFile(String pid) throws IOException {
		String hql = "from ProjectFile where  invalid=false and id=:id";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", pid);
		List<ProjectFile> list = this.query(hql, map);
		ProjectFile pf = list.get(0);
		this.deleteProjectFile(pf);
	}

}
